package com.xk._01基础篇._12堆.heap;

import java.util.Comparator;

/**
 * @description: 堆 抽象类
 * @author: xu
 * @date: 2022/9/29 0:44
 */
public abstract class AbstractHeap<E> implements Heap<E> {
    protected int size;
    protected Comparator<E> comparator;

    public AbstractHeap(){ this(null); }
    public AbstractHeap(Comparator<E> comparator){
        this.comparator = comparator;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    protected int compare (E e1, E e2) {
        return comparator != null ? comparator.compare(e1, e2)
                : ((Comparable<E>) e1).compareTo(e2);
    }
}
